package com.myspace.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.myspace.sdk.MSRequest;
import com.myspace.sdk.MSSDK;
import com.myspace.sdk.MSRequest.MSRequestCallback;

public class MSVideoActivity extends MSBaseActivity {

	private static final String TAG = "MSVideoActivity";
	private static final int VIDEO_DIALOG = 1;
	private static final int PROGRESS_DIALOG = 2;
	private static final int ACTIVITY_RESULT_VIDEO_SELECTED = RESULT_FIRST_USER;
	
	private Button mUploadButton;
	private Button mSelectButton;
	private ImageView mImageView;
	private Uri mVideoUri;
	private ProgressDialog mProgressDialog;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setPageName(R.string.PAGE_TITLE_VIDEO);
		super.setMainView(R.layout.video);
		
		mSelectButton = (Button) findViewById(R.id.selectVideoButton);
		mSelectButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showVideoSourceDialog();
			}
		});
		
		mUploadButton = (Button) findViewById(R.id.uploadVideoButton);
        mUploadButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				uploadVideo();
			}
		});
        
        mImageView = (ImageView) findViewById(R.id.videoImageView);
    }
	
	private void uploadVideo() {
		try {
			if(mVideoUri != null) {
				showDialog(PROGRESS_DIALOG);
				
				Map<String, String> queryParams = new HashMap<String, String>();
				queryParams.put("source", "mobile");
		    	queryParams.put("caption", "test video");
		    	queryParams.put("description", "test description");
		    	queryParams.put("msCategories", "11");
		    	queryParams.put("tags", "test tag");
		    	
				MSSDK.uploadVideo(this, queryParams, mVideoUri, new UploadVideo_MSRequestCallback());
			}
			else {
				Log.d(TAG, "Video uri is null");
				showVideoSourceDialog();
			}
		} catch (Exception e) {
			Log.d(TAG, e.toString());
		}
	}
	
	private void showVideoSourceDialog() {
		showDialog(VIDEO_DIALOG);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case VIDEO_DIALOG:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Choose Video Source")
				   .setPositiveButton("Gallery", videoSourcePositiveListener )
				   .setNeutralButton("Camera", videoSourceNeutralListener )
				   .setNegativeButton("Cancel", videoSourceNegativeListener );
			return builder.create();
		case PROGRESS_DIALOG:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setMessage("Please wait while uploading...");
			mProgressDialog.setIndeterminate(true);
			mProgressDialog.setCancelable(true);
			return mProgressDialog;
		}
		return null;
	}
	
	DialogInterface.OnClickListener videoSourcePositiveListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			showNativeVideoGallery();
		}
	};

	DialogInterface.OnClickListener videoSourceNeutralListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		}
	};

	DialogInterface.OnClickListener videoSourceNegativeListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			finish();
		}
	};
	
	private void showNativeVideoGallery() {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("video/*");
		startActivityForResult(intent, ACTIVITY_RESULT_VIDEO_SELECTED);			
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		
		if (requestCode == ACTIVITY_RESULT_VIDEO_SELECTED) {
			if (resultCode == RESULT_OK) {
				Uri uri = intent.getData();
				try {
					mImageView.setImageResource(R.drawable.generic_video_thumb);
					mVideoUri = uri;
				} catch(Exception e) {
					Log.d(TAG, e.toString());
				}
			}
		}
	}
	
	private class UploadVideo_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			dismissDialog(PROGRESS_DIALOG);
			showToast(error.getMessage());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			dismissDialog(PROGRESS_DIALOG);
			mVideoUri = null;
			showToast("Upload Successful");
		}
	}
}
